/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package Controller.Admin;

import Model.DAO.AgentDAO;
import Model.DAO.AgentImp;
import Model.Pojo.Agents;
import Model.Pojo.Group;
import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;
import javax.faces.application.FacesMessage;
import javax.faces.application.ViewHandler;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.SessionScoped;
import javax.faces.bean.ViewScoped;
import javax.faces.component.UIViewRoot;
import javax.faces.context.FacesContext;

/**
 *
 * @author luong
 */
@ManagedBean
//@SessionScoped
@ViewScoped
public class LoginController implements Serializable{
    private static final long serialVersionUID = 1L;
    private Agents agent;
    private AgentDAO agentdao=new AgentImp();
    private List<Agents> listAgent= new ArrayList<Agents>(agentdao.GetListAgents()) ;
    private String userNameAgents;
    private String passAgents;
    private String nameAgents;
    private String mobileAgents;
    private String emailAgents;
    private String addressAgents;

    public Agents getAgent() {
        return agent;
    }

    public void setAgent(Agents agent) {
        this.agent = agent;
    }
    
    public List<Agents> getListAgent() {
        return listAgent;
    }

    public void setListAgent(List<Agents> listAgent) {
        this.listAgent = listAgent;
    }
    private String Group;

    public String getGroup() {
        return Group;
    }

    public void setGroup(String Group) {
        this.Group = Group;
    }

    
    public String getNameAgents() {
        return nameAgents;
    }

    public void setNameAgents(String nameAgents) {
        this.nameAgents = nameAgents;
    }

    public String getMobileAgents() {
        return mobileAgents;
    }

    public void setMobileAgents(String mobileAgents) {
        this.mobileAgents = mobileAgents;
    }

    public String getEmailAgents() {
        return emailAgents;
    }

    public void setEmailAgents(String emailAgents) {
        this.emailAgents = emailAgents;
    }

    public String getAddressAgents() {
        return addressAgents;
    }

    public void setAddressAgents(String addressAgents) {
        this.addressAgents = addressAgents;
    }

    public String getUserNameAgents() {
        return userNameAgents;
    }

    public void setUserNameAgents(String userNameAgents) {
        this.userNameAgents = userNameAgents;
    }

    public String getPassAgents() {
        return passAgents;
    }

    public void setPassAgents(String passAgents) {
        this.passAgents = passAgents;
    }
    
    public boolean isLoggedIn() {
        return agent.getIdAgents()!= null;
    }
    public String editAction(Agents agent) {
 
	agent.setEditable(true);
	return null;
    }
    public String entrer() {
        agent = agentdao.Entrer(userNameAgents, passAgents);
        if (agent!=null) {
            isPostback();
            return "home";
        }
        else return "login";           
    }
    public String ajouter()
    {   
        agent = new Agents();
        int i = Integer.parseInt(Group);
        Group g = new Group(i);
        agent.setGroup(g);
        agent.setEmailAgents(emailAgents);
        agent.setNameAgents(nameAgents);
        agent.setPassAgents(passAgents);
        agent.setUserNameAgents(userNameAgents);
        agent.setAddressAgents(addressAgents);
        agent.setMobileAgents(mobileAgents);
        if (agentdao.AjouterUser(agent)) {
            //isPostback();
         return "thanhcong.html";
        }
        else return "thatbai.html";  
    }
    public String suprimer(Agents agent)
    {        
         if (agentdao.SuprimerUser(agent)) {
             refresh();
         return "home";
        }
        else return "suprimer";  
        
    }
    public static boolean isPostback() {
        return FacesContext.getCurrentInstance().isPostback();
    }
    public String updateUser()
    {
        agent= new Agents();
        
        int i = Integer.parseInt(Group);
        Group g = new Group(i);
        int idAgent=agentdao.CheckAgents(userNameAgents).getIdAgents();
        agent.setIdAgents(idAgent);
        agent.setGroup(g);
        agent.setEmailAgents(emailAgents);
        agent.setNameAgents(nameAgents);
        agent.setPassAgents(passAgents);
        agent.setUserNameAgents(userNameAgents);
        agent.setAddressAgents(addressAgents);
        agent.setMobileAgents(mobileAgents);      
         if (agentdao.UpdateUser(agent)) {
         return "thanhcong.html";
        }
        else return "thatbai.html";  
    }
    public String tableUpdateUser(Agents agents){
        for (Agents agentst : listAgent){
            agentst.setEditable(false);           
        }
        String s = agents.getGroup().getNameGroup();
        if (s.equals("admin")){
            Group g = new Group(1);
            
            agents.setGroup(g);
        }
        else {
            Group g = new Group(2);  
            agents.setGroup(g);
        }
        
            agentdao.UpdateUser(agents);
            Group g =new Group(s);
            agents.setGroup(g);
            FacesContext context = FacesContext.getCurrentInstance();
        context.addMessage(null, new FacesMessage(FacesMessage.SEVERITY_INFO,
            "", "Lưu thành công!"));
        return null;
    }
    public void refresh(){
        FacesContext context = FacesContext.getCurrentInstance();
        String viewId = context.getViewRoot().getViewId();
        ViewHandler handler = context.getApplication().getViewHandler();
        UIViewRoot root = handler.createView(context, viewId);
        root.setViewId(viewId);
        context.setViewRoot(root);
    }
    
    
}
